package com.dingli.servlet.ajax;

import com.dingli.DAO.LineDAO;
import com.dingli.DAO.TicketDAO;
import com.dingli.utils.Tools;
import com.sun.javafx.scene.control.skin.SplitPaneSkin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/createTicket")
public class CreateTicketServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String tktLv = req.getParameter("tktLv");
        String price = req.getParameter("price");
        String num = req.getParameter("num");

        String name = req.getParameter("name");
        String passby = req.getParameter("passby");

        if(Tools.isTheseEmpty(tktLv,price,num,name,passby)){
            resp.getWriter().write("false");
            return;
        }
        int sitenum = passby.split("-").length;

        if(sitenum <= 1){
            resp.getWriter().write("false");
            return;
        }

        LineDAO lineDAO = new LineDAO();
        int lineID = lineDAO.addLine(name, passby, sitenum);
        if(Tools.isEmpty(lineID+"") || lineID <= 0){
            resp.getWriter().write("false");
            return;
        }
        TicketDAO ticketDAO = new TicketDAO();
        int tktID = ticketDAO.createTicket(tktLv, lineID, num, price);
        if(Tools.isEmpty(tktID+"") || tktID <= 0){
            resp.getWriter().write("false");
            return;
        }
        resp.getWriter().write("true");
    }
}
